<template>
	<view>
		<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
		<u--form labelPosition="left" labelWidth="80" :model="info" :rules="rules" ref="uForm">
			<u-form-item label="手机号" prop="mobile" borderBottom ref="item1">
				<u--input placeholder="请输入手机号" border="surround" v-model="info.mobile"></u--input>
				<!-- <u--input v-model="model1.userInfo.name" border="none"></u--input> -->
			</u-form-item>


			<u-form-item label="验证码" prop="code" borderBottom ref="item1">
				<view class="u-demo-block__content" style="margin-top: 15px;">

					<u--input placeholder="输入验证码" v-model="info.code">
						<template slot="suffix">
							<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
							<u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button>
						</template>

					</u--input>

				</view>

			</u-form-item>
			<u-button class="but" type="primary" @click="submit">登录</u-button>
		</u--form>

	</view>
</template>

<script>
	import {login} from '@/utils/api/login.js';
	import {getSMSCode} from '@/utils/api/public.js';
	export default {
		data() {
			return {
				info: {
					mobile: "",
					code: "",
				},
				rules: {
					mobile: {
						type: 'string',
						required: true,
						message: '请输入手机号',
						trigger: ['blur', 'change']
					},
					code: {
						type: 'string',
						max: 4,
						required: true,
						message: '请输入验证码',
						trigger: ['blur', 'change']
					},
				},
				tips: '',
				value: ''
			}
		},
		onLoad() {
	
		},
		methods: {
			submit() {
				this.$refs.uForm.validate().catch(errors => {
					uni.$u.toast('参数错误')
					return
				})

				login(this.info).then(res => {
					if (res.data.code != 0) {
						return
					}
					
					uni.setStorageSync('token', res.data.data)			
					uni.redirectTo({
						url: '/pages/mine/mine',
					});
					return;
				})
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					getSMSCode({
						mobile: this.info.mobile,
						from: 'login',
					}).then(res => {
						if (res.data.code != 0) {
							return
						}

						setTimeout(() => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						}, 2000);
					})

				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
		},
	};
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 20px;
	}

	h1 {
		margin-bottom: 30px;
	}

	.form-row {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 20px;
		width: 100%;
	}

	.label {
		width: 80px;
		font-size: 16px;
		text-align: right;
		margin-right: 10px;
	}

	.input-container {
		display: flex;
		align-items: center;
		justify-content: center;
		flex: 1;
	}

	.input-field,
	.input-field2 {
		width: 240px;
		height: 30px;
		line-height: 30px;
		border: 1px solid #ccc;
		border-radius: 4px;
		padding-left: 10px;
	}

	.input-field2 {
		width: 150px;
	}

	.button,
	.button2,
	.button3 {
		background-color: deepskyblue;
		height: 30px;
		line-height: 30px;
		border: none;
		color: white;
		padding: 0 15px;
		border-radius: 4px;
		cursor: pointer;
	}

	.button2 {
		background-color: #f0f0f0;
		color: #8f8f94;
	}

	.button3 {
		width: 150px;
	}

	.but {
		margin-top: 20rpx;
		width: 400rpx;
	}
</style>